from server import *
from bottle import route, run, template

prefix = "/v1/<email>/<password>"

@route(prefix + "/register/<username>")
def register(email='', password='', username=''):
    user_id = dao.register(email, username, password)
    if user_id:
        return "OK;" + str(user_id)
    else:
        return "FAIL"

@route(prefix + "/login")
def login(email='', password=''):
    user_id = dao.login(email, password)
    if user_id:
        return "OK;" + user_id
    else:
        return "FAIL"

@route(prefix + "/games/<latitude>/<longitude>/<offset>/<size>")
def games(email='', password='', latitude='', longitude='', offset='', size=''):
    game_list = dao.facade(email, password, int(offset), int(size))
    output = template('templates/games', games=game_list)
    return output

@route(prefix + "/game_status")
def status(email='', password=''):
    game_id, game_max_step, game_statuses = dao.game_status(email, password)
    output = template('templates/status', game_id=game_id,
            game_max_step=game_max_step, status=game_statuses)
    return output

@route(prefix + "/scores")
def scores(email='', password=''):
    user_score, high_scores = dao.global_high_scores(email, password)
    output = template('templates/high_scores', user_score=user_score,
            high_scores=high_scores)
    return output

@route(prefix + "/game_scores/<my_scores>")
def game_scores(email='', password='', my_scores=''):
    user_score, game_scores = dao.game_scores(email, password)
    if game_scores:
        output = template('templates/game_scores', user_score=user_score,
            game_scores=game_scores)
        return output

@route(prefix + "/hints/<next>")
def hints(email='', password='', next=0):
    hints = dao.give_hints(email, password, int(next))
    print hints
    output = template('templates/hints', hints=hints)
    return output

@route(prefix + "/location/<latitude>/<longitude>")
def location(email='', password='', latitude='', longitude=''):
    dist, name, next_hint = dao.submit_location(email, password, int(latitude), int(longitude))
    if dist == 0:
        return str(dist) + ";" + str(name) + ";" + str(next_hint)
    return str(dist)

@route(prefix + "/join/<game_id>")
def join(email='', password='', game_id=''):
    if dao.join_game(email, password, int(game_id)):
        return "OK"
    else:
        return "FAIL"

@route(prefix + "/drop")
def drop(email='', password=''):
    if dao.drop_game(email, password):
        return "OK"
    else:
        return "FAIL"

dao = DAO()
run(host='localhost', port=8080, debug=True)

